#pragma once 


#include <istream>
#include <pthread.h>
#include <signal.h>
#include "TcpServer.hpp"
#include "Prorocol.hpp"
#include "log.hpp"
#include "Task.hpp"
#include "Pthreadpopl.hpp"

class HttpSever{
  private:
    bool _stop; // 是否在运行
    int _port;
  public:
    HttpSever(int port = 8081)
      :_port(port),
      _stop(false)
    {}

    void InitSever()
    {
      signal(SIGPIPE , SIG_IGN);
      //  _tcp_server = TcpServer::getinstance(_port);
    }

    void Loop()
    {
      TcpServer* tsvr = TcpServer::getinstance(_port);
      while(!_stop)
      {
        LOG(INFO , "Loop begin");
        struct sockaddr_in peer;
        socklen_t len = sizeof(peer);
        int sock = accept(tsvr->Sock() ,(struct sockaddr*)&peer , &len);
        if (sock < 0)
        {
          continue; // 获取失败了不暂停
        }

        Task task(sock);
        ThreadPool::getinstance()->PushTak(task); 
      }
    }
    ~HttpSever()
    {}
};
